package com.cat.binaryAnswer;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/minimum-time-to-complete-trips/
 * @create 2025/8/11 08:47
 * @since JDK17
 */

public class Solution06 {
    boolean check(int[] times, int total, long target) {
        int n = times.length;
        long sum = 0;
        for (int i = 0; i < n; i++) {
            sum += target / times[i];
        }
        return sum >= total;
    }

    public long minimumTime(int[] time, int totalTrips) {
        int n = time.length, min = time[0];
        for (int i = 1; i < n; i++) {
            min = Math.min(min, time[i]);
        }
        long l = -1, r = (long) totalTrips * min, mid;
        while (l + 1 < r) {
            mid = l + (r - l) / 2;
            if (check(time, totalTrips, mid)) {
                r = mid;
            } else {
                l = mid;
            }
        }
        return r;
    }
}
